package org.jeecg.modules.app.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.app.entity.Food;
import org.jeecg.modules.app.entity.Order;
import org.jeecg.modules.app.entity.OrderFood;
import org.jeecg.modules.app.mapper.FoodMapper;
import org.jeecg.modules.app.mapper.ShopMapper;
import org.jeecg.modules.app.service.OrderService;
import org.jeecg.modules.app.vo.ConsumeVO;
import org.jeecg.modules.app.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李圳川
 * @version 1.0
 * @date 2022/6/20 0:48
 * @content
 */

@RestController
@RequestMapping("/schoolOrder/app/order")
@Api(tags="app订单")
@Slf4j
public class OrderController {


    @Autowired
    private OrderService foodOrderService;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private FoodMapper foodMapper;

    /**
     *  订单查询
     * @param customerId
     * @return
     */
    @ApiOperation("app订单查询")
    @GetMapping("/findAllFoodByCustomerId")
    public Result<?> findAllOrderByCustomerId(String customerId){
        //System.out.println(customerId);
        List<Order> orderList = foodOrderService.findAllOrderByCustomerId(customerId);
        return Result.success("200","查询成功", orderList);
    }

    /**
     *  状态修改
     * @param order
     * @return
     */
    @ApiOperation("app订单状态修改")
    @PostMapping("/updateOrderById")
    public Result<?> updateOrderById(@RequestBody Order order){
        foodOrderService.updateOrderById(order.getId(),order.getStatus()+"");
      //  System.out.println(order);
       if(order.getStatus().equals("1")){
           shopMapper.updateSellCountInOrderById(order.getShopId(),order.getNum());
           ArrayList<OrderFood> foodList = order.getFoodList();
           for(OrderFood food:foodList){
               foodMapper.upDateSellCountInFood(food.getFoodId(),food.getNum());
           }
       }
        return Result.success("200","修改成功");
    }

    /**
     * 下单
     * @param order
     * @return
     */
    @ApiOperation("app下单")
    @PostMapping("/order")
    public Result<?> order(@RequestBody Order order){
        String orderId = foodOrderService.order(order);
       // System.out.println(order);
        if(order.getStatus().equals("1")){
            shopMapper.updateSellCountInOrderById(order.getShopId(),order.getNum());
            ArrayList<OrderFood> foodList = order.getFoodList();
            for(OrderFood food:foodList){
                foodMapper.upDateSellCountInFood(food.getFoodId(),food.getNum());
            }
        }
        return Result.success("200","下单成功",orderId);
    }

    /**
     * 消费记录查询
     * @param id
     * @return
     */
    @ApiOperation("app查询顾客消费记录")
    @GetMapping("/selectCustomerConsumeById")
    public Result<?> selectCustomerConsumeById(String id){
        List<ConsumeVO> consumeVOList = foodOrderService.selectCustomerConsumeById(id);
        return Result.success("200","查询成功",consumeVOList);
    }


}
